package com.zhou.listener;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.liulishuo.filedownloader.FileDownloader;
import com.zhou.listener.bean.KeyGen;
import com.zhou.listener.library.ListenerManager;
import com.zhou.listener.baselistener.DetailFileDownloadListener;
import com.zhou.listener.library.utils.KeyUtils;
import com.zhou.listener.listener.ListDownloadListener;
import com.zhou.listener.utils.AdapterUtils;
import com.zhou.listener.library.utils.IKey;
import com.zhou.listener.library.vh.BaseDownloadViewHolder;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lvlufei on 2018/1/19.
 * 套餐资料-适配器
 */

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.VH> {
    String path;
    private ArrayList<MyBean> arrayList;
    public RecyclerAdapter(ArrayList<MyBean> arr,Context ctx){
        this.arrayList = arr;
        path = ctx.getCacheDir().getPath()+File.separator + "image_manager_disk_cache";
    }

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item2,viewGroup,false);
        return new VH(v);
    }

    @Override
    public void onBindViewHolder(@NonNull final VH vh, final int i) {
        Log.i("---->","onBindViewHolder"+arrayList.get(i).id);
        KeyUtils.addKeyGens(vh,new KeyGen(arrayList.get(i).id+""));
        AdapterUtils.updateListener(vh.keys,new ListDownloadListener(vh));
//        Log.i("---->","绑定ViewHolder"+"vh.id"+vh.keyGen.getKey());
        vh.name.setText(arrayList.get(i).name);
//        AdapterUtils.updateListener(vh,new ListDownloadListener(vh));
        vh.name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(i==arrayList.size()-1){
                    vh.name.getContext().startActivity(new Intent(vh.name.getContext(),EaseRecyclerActivity.class));
                    return;
                }
                FileDownloader.getImpl().create(arrayList.get(i).url)
                        .setPath(path)
                        .setForceReDownload(true)
                        .setListener(ListenerManager.getInstance().addListener(vh.keys.get(0),
                                new DetailFileDownloadListener(new ListDownloadListener(vh))
                        )).start();
            }
        });
    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }

    @Override
    public void onViewDetachedFromWindow(@NonNull VH holder) {
        super.onViewDetachedFromWindow(holder);
        Log.i("---->","onViewDetachedFromWindow");
        AdapterUtils.clearPreListener(holder.keys);
    }

    public static class VH extends RecyclerView.ViewHolder implements BaseDownloadViewHolder {
        public List<IKey> keys;
        public Button name;
        public VH(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.name);
        }

        @Override
        public List<IKey> getKeys() {
            return keys;
        }

        @Override
        public void setKeys(List<IKey> keys) {
            this.keys = keys;
        }
    }
}
